package com.ccys.cms.controller.productAndService;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.productAndService.SupplierService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.dto.web.productAndService.SupplierDto;
import com.ccys.db.entity.TblSupplier;
import com.ccys.db.vo.web.productAndService.SupplierVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "供应商管理")
public class SupplierController {


    @Resource
    private SupplierService supplierService;

    @PostMapping("supplier")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(HttpServletRequest request, TblSupplier supplier) {
        //生成code
        if (StringUtils.isEmpty(supplier.getId())) {
            long code = IdUtil.getSnowflakeNextId();
            supplier.setCode(code + "");
        }
        int i = supplierService.insertOrUpdate(supplier);
        return ResponseResult.update(i);
    }

    @PutMapping("supplier")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(HttpServletRequest request, TblSupplier supplier) {
        //生成code
        if (StringUtils.isEmpty(supplier.getId())) {
            long code = IdUtil.getSnowflakeNextId();
            supplier.setCode(code + "");
        }
        int i = supplierService.insertOrUpdate(supplier);
        return ResponseResult.update(i);
    }



    @GetMapping("supplier")
    @ApiOperation(value = "查询供应商分页", notes = "排序字段 t1.code(supplier.no) / t1.name / t3.group_name / t1.contact_person / t1.status /t2. user_fullname / t1.created_on ")
    public ResponseResult<PageInfo<SupplierVo>, Object> query(SupplierDto dto) {
        return supplierService.query(dto);
    }



    @DeleteMapping("supplier")
    @ApiOperation("根据id删除")
    @Transactional
    public ResponseResult delete(@RequestParam("id") String id) {
        //逻辑删除自己控制
        TblSupplier supplier = new TblSupplier();
        supplier.setId(id);
        supplier.setDelFlag(NumEnum.DEL.getCode());
        return ResponseResult.update(supplierService.updateEntityById(supplier));
    }


    @GetMapping("supplier/list")
    @ApiOperation(value = "查询集合", notes = "type: 可用 service 或者 product 的供应商（非必填）")
    public ResponseResult<TblSupplier, Object> getList(@RequestParam(required = false) String type) {


        List<TblSupplier> list = supplierService.findListByLimit(TblSupplier.class, new ArrayList<String>() {{
            add(" del_flag = 0 ");
            if (StringUtils.isNotEmpty(type)) {
                add(" FIND_IN_SET('" + type + "',type)");
            }
        }}, "created_on_utc desc");

        return ResponseResult.success(list);

    }
}
